package cn.stylefeng.guns.modular.util.itext;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.modular.business.custom.enums.PickCarTypeEnum;
import cn.stylefeng.guns.modular.business.custom.enums.TakeCarTypeEnum;
import cn.stylefeng.guns.modular.business.frequency.orderpick.model.result.PkgOrderPick;
import cn.stylefeng.guns.modular.business.frequency.ordersub.entity.OrderSub;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderVO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderSub;
import cn.stylefeng.guns.modular.business.frequency.ordertake.model.result.PkgOrderDelivery;
import cn.stylefeng.guns.modular.business.frequency.transport.model.result.PkgTransport;
import cn.stylefeng.guns.modular.business.frequency.transport.model.result.PkgTransportLevel;
import cn.stylefeng.guns.modular.business.frequency.transportleveldetail.entity.TransportLevelDetail;
import cn.stylefeng.guns.modular.util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.index.qual.SameLen;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author jzy
 * @Date 2019/12/7 0007
 **/

@Slf4j
public class ItextUtil {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

    /**
     * 创建保单
     * @param order
     * @param contractPath
     * @param logoImg
     * @param signImg
     * @param waterImg
     */
    public static void createPDFInsurance(OrderVO order,
            String company,
            String contractPath,
            URL logoImg,
            URL signImg,
            URL waterImg) {
        Document document = new Document(PageSize.A4, 50, 50, 20, 50);
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font14 = new Font(baseFont, 14);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Font font8 = new Font(baseFont, 8, Font.NORMAL);
            Font font8Bold = new Font(baseFont, 8, Font.BOLD);
            Font font4 = new Font(baseFont, 4, Font.NORMAL);
//            HeaderFooter headerFooter = new HeaderFooter(new Phrase("这仅仅是一个页眉", font20), false);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(contractPath + "保单-" + order.getLinkmanName() + ".pdf"));
            // 只读
            writer.setEncryption(null, "adwl8888".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            // 水印
            writer.setPageEvent(new ImgWatermarkEvent(waterImg));
            document.open();
            Image img = Image.getInstance(logoImg);
            img.setAlignment(Element.ALIGN_LEFT);
            img.scaleToFit(500, 50);
            document.add(img);
            LineSeparator line = new LineSeparator(1f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, -5f);
            document.add(line);
            Paragraph orderNo = new Paragraph("保险单号：ANIB90012720Q000700M", font8);
            orderNo.setSpacingBefore(5f);
            orderNo.setAlignment(Element.ALIGN_RIGHT);
            document.add(orderNo);
            Paragraph title = new Paragraph("其他国内货运险电子保险单", font14);
            title.setAlignment(Element.ALIGN_CENTER);
            title.setSpacingBefore(-4);
            title.setSpacingAfter(4);
            document.add(title);
            document.add(line);
            Paragraph paragraph1 = new Paragraph("中国太平洋财产保险股份有限公司（以下称保险人）根据投保人要求，在投保人向保险人缴付约定的保险费后，按照国内水路、陆路货物运输保险条款及保险凭证所注明的其他条件，对下列货物承保运输险。", font10);
            paragraph1.setSpacingAfter(3);
            document.add(paragraph1);
            document.add(line);
            LineSeparator line2 = new LineSeparator(1.5f, 100, BaseColor.GRAY, Element.ALIGN_CENTER, -7f);
            document.add(line2);
            Paragraph paragraph2 = new Paragraph("被保险人："+company+"                投保人：    "+company, font10);
            paragraph2.setSpacingAfter(3);
            paragraph2.setSpacingBefore(4);
            document.add(paragraph2);
            document.add(line);
            document.add(line2);
            Paragraph paragraph3 = new Paragraph();
            Chunk chunk = new Chunk("起运地：     " + order.getStartCode() + "                ", font10);
            Chunk chunk1 = new Chunk("                                                         运输工具：", font10);
            Chunk chunk2 = new Chunk("（船名航次车号）", font4);
            paragraph3.add(chunk);
            paragraph3.add(chunk1);
            paragraph3.add(chunk2);
            paragraph3.setSpacingAfter(4);
            paragraph3.setSpacingBefore(2);
            document.add(paragraph3);

            //表格
            PdfPTable firstRowTable = new PdfPTable(9);
            firstRowTable.setWidthPercentage(100);//设置长度占纸张比例
            firstRowTable.setTotalWidth(new float[]{0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.1f});
            PdfPCell cell1 = mircoSoftFont("货票运单\n" + "号码", font10, 30, true, false, false);
            cell1.setRowspan(2);
            firstRowTable.addCell(cell1);
            PdfPCell cell2 = mircoSoftFont("货物名称", font10, 30, true, false, false);
            cell2.setRowspan(2);
            firstRowTable.addCell(cell2);
            PdfPCell cell3 = mircoSoftFont("运输方式", font10, 30, true, false, false);
            cell3.setRowspan(2);
            firstRowTable.addCell(cell3);
            PdfPCell cell4 = mircoSoftFont("件数/重量", font10, 30, true, false, false);
            cell4.setRowspan(2);
            firstRowTable.addCell(cell4);
            PdfPCell cell5 = mircoSoftFont("目的地", font10, 30, true, false, false);
            cell5.setRowspan(2);
            firstRowTable.addCell(cell5);
            PdfPCell cell6 = mircoSoftFont("起运日期", font10, 30, true, false, false);
            cell6.setRowspan(2);
            firstRowTable.addCell(cell6);
            PdfPCell cell7 = mircoSoftFont("保险金额", font10, 30, true, false, false);
            cell7.setRowspan(2);
            firstRowTable.addCell(cell7);

            PdfPTable itable;
            PdfPCell icell;
            itable = new PdfPTable(2);
            icell = mircoSoftFont("保险费率", font10, 15, true, false, false);
            icell.setColspan(2);
            itable.addCell(icell);
            icell = mircoSoftFont("基本险", font10, 15, true, false, false);
            itable.addCell(icell);
            icell = mircoSoftFont("综合险", font10, 15, true, false, false);
            itable.addCell(icell);
            firstRowTable.addCell(new PdfPCell(itable));


            PdfPCell cell9 = mircoSoftFont("保险费", font10, 30, false, false, false);
            cell9.setRowspan(2);
            firstRowTable.addCell(cell9);
            document.add(firstRowTable);

            for (OrderSub orderSub : order.getOrderSubList()) {
                PdfPTable content = new PdfPTable(10);
                content.setWidthPercentage(100);//设置长度占纸张比例
                content.setTotalWidth(new float[]{0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f});
                PdfPCell contentCell1 = mircoSoftFont("", font10, 25, false, false, false);
//                cell1.setRowspan(2);
                content.addCell(contentCell1);
                PdfPCell contentCell2 = mircoSoftFont(order.getOrderSubList().get(0)
                        .getCarName(), font10, 25, false, false, false);
//                cell2.setRowspan(2);
                content.addCell(contentCell2);
                PdfPCell contentCell3 = mircoSoftFont("联运", font10, 25, false, false, false);
//                cell3.setRowspan(2);
                content.addCell(contentCell3);
                PdfPCell contentCell4 = mircoSoftFont("1台", font10, 25, false, false, false);
//                cell4.setRowspan(2);
                content.addCell(contentCell4);
                PdfPCell contentCell5 = mircoSoftFont(order.getDestinationCode(), font10, 25, false, false, false);
//                cell5.setRowspan(2);
                content.addCell(contentCell5);
                PdfPCell contentCell6 = mircoSoftFont("As Per B/L", font10, 25, false, false, false);
//                cell6.setRowspan(2);
                content.addCell(contentCell6);
                String carCost = orderSub.getCarCost() == null ? "" : (orderSub.getCarCost()
                        .multiply(new BigDecimal("10000")).toString() + " RMB");
                PdfPCell contentCell7 = mircoSoftFont(carCost, font10, 25, false, false, false);
//                cell7.setRowspan(2);
                content.addCell(contentCell7);
                PdfPCell contentCell8 = mircoSoftFont("AS ARRANGED", font10, 25, false, false, false);
//                cell7.setRowspan(2);
                content.addCell(contentCell8);
                PdfPCell contentCell9 = mircoSoftFont("", font10, 25, false, false, false);
//                cell9.setRowspan(2);
                content.addCell(contentCell9);
                PdfPCell contentCell10 = mircoSoftFont("AS ARRANGED", font10, 25, false, false, false);
//                cell9.setRowspan(2);
                content.addCell(contentCell10);

                document.add(content);
            }

            Paragraph paragraph4 = new Paragraph("保险费：（大写）人民币 AS ARRANGED                                         ￥   AS ARRANGED", font10);
            paragraph4.setSpacingAfter(3);
            paragraph4.setSpacingBefore(4);
            document.add(paragraph4);
            document.add(line);
            document.add(line2);

            Paragraph paragraph5 = new Paragraph("特别约定：本电子保单凭合法索赔人提出的第一次索赔有效", font8);
            paragraph5.setSpacingAfter(10);
            paragraph5.setSpacingBefore(4);
            document.add(paragraph5);

            Paragraph paragraph6 = new Paragraph("详情见附页", font8);
            paragraph6.setSpacingAfter(30);
            paragraph6.setSpacingBefore(8);
            document.add(paragraph6);
            document.add(line);
            document.add(line2);

            Paragraph paragraph7 = new Paragraph();
            Chunk chunk4 = new Chunk("制单人：何美艳                                                                                  ", font10);
            chunk4.setUnderline(0.1f, -2f);
            Chunk chunk5 = new Chunk("中国太平洋财产保险股份有限公司   ", font10);
            Chunk chunk6 = new Chunk("宁波分公司                       ", font10);
            chunk6.setUnderline(0.1f, -2f);
            paragraph7.add(chunk4);
            paragraph7.add(chunk5);
            paragraph7.add(chunk6);
            paragraph7.setSpacingAfter(1);
            paragraph7.setSpacingBefore(3);
            document.add(paragraph7);

            //签章图片
            // -----------------------
            Image image1 = Image.getInstance(signImg);
            image1.setAbsolutePosition(360, 800 - (300 + 30 * order.getOrderSubList().size()));
            image1.scaleToFit(90, 90);
            document.add(image1);

            Paragraph paragraph8 = new Paragraph("公司地址：宁波市义和路95号                                                   " + "（签章）                                   " + simpleDateFormat.format(new Date()) + "", font10);
            paragraph8.setSpacingAfter(30);
            document.add(paragraph8);

            document.newPage();

            document.add(img);
            document.add(line);

            Paragraph paragraph9 = new Paragraph();
            Chunk chunk7 = new Chunk("(本页为保单续页，单独出具无效)", font8);
            document.add(chunk7);

            Chunk chunk8 = new Chunk("保险单号：ANIB90012720Q000700M", font8);
            paragraph9.add(chunk8);
            paragraph9.setAlignment(Element.ALIGN_RIGHT);
            paragraph9.setSpacingBefore(-16);
            document.add(paragraph9);

            document.add(title);

            Paragraph paragraph10 = new Paragraph("免赔：\n免赔率保额的0%按该险位全部货物计算\n附加条款：\n1、国内水路、陆路货物运输保险条款基本险\n锈损、刮擦风险除外条款\n1、每次事故" + "绝对免赔人民币1000元或者损失金额的5%（包括全损），两者以高者为准。\n" + "2、任何情况下的火灾事故免赔为损失金额的20%。\n" + "根据国税总局要求，保险行业2016年5月1日起正式实行增值税，以上保险为含税价，应税产品的税率为6%。具体增值税相关事宜\n" + "，请咨询我司各网点。\n" + "理赔服务提示：如您在保险合同生效期间不幸发生保险事故，请在48小时内通过热线电话95500报案，我们将在接到您报案后的1个工作日内与您\n" + "联系，指导并协助您办理理赔事宜。", font8Bold);
            paragraph10.setSpacingBefore(5f);
            paragraph10.setLeading(9f);//设置行间距
            paragraph10.setSpacingAfter(30f);
            document.add(paragraph10);

            document.add(line);
            document.add(line2);

            Paragraph paragraph11 = new Paragraph();
            Chunk chunk9 = new Chunk("签单公司信息                                                                                  ", font10);
            chunk9.setUnderline(0.1f, -2f);
            Chunk chunk10 = new Chunk("中国太平洋财产保险股份有限公司   ", font10);
            Chunk chunk11 = new Chunk("宁波分公司                       ", font10);
            chunk11.setUnderline(0.1f, -2f);
            paragraph11.add(chunk9);
            paragraph11.add(chunk10);
            paragraph11.add(chunk11);
            paragraph11.setSpacingAfter(1);
            paragraph11.setSpacingBefore(3);
            document.add(paragraph11);

            Paragraph paragraph12 = new Paragraph();
            Chunk chunk12 = new Chunk("地址：宁波市和义路95号                                                           ", font10);
            chunk12.setUnderline(0.1f, -2f);
            paragraph12.add(chunk12);
            document.add(paragraph12);

            Paragraph paragraph13 = new Paragraph();
            Chunk chunk13 = new Chunk("邮编：315000                                                                                    ", font10);
            chunk13.setUnderline(0.1f, -2f);
            paragraph13.add(chunk13);
            document.add(paragraph13);

            Paragraph paragraph14 = new Paragraph();
            Chunk chunk14 = new Chunk("电话：95500                     传真： 87246016                           ", font10);
            chunk14.setUnderline(0.1f, -2f);
            Chunk chunk15 = new Chunk("  （公司签章）                                                                                                ", font10);
            chunk15.setUnderline(0.1f, -2f);
            paragraph14.add(chunk14);
            paragraph14.add(chunk15);
            paragraph14.setSpacingAfter(5);
            paragraph14.setSpacingBefore(3);
            document.add(paragraph14);

            // -------------1012--------
            image1.setAbsolutePosition(360, 500);
            image1.scaleToFit(90, 90);
            document.add(image1);
            // -------------1012---------

            //表格
            PdfPTable table = new PdfPTable(8);
            table.setWidthPercentage(100);//设置长度占纸张比例
            table.setTotalWidth(new float[]{0.1f, 0.3f, 0.1f, 0.4f, 0.2f, 0.3f, 0.2f, 0.3f});
            PdfPCell cell8;
            cell8 = mircoSoftFont("制单", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("何美艳", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("经办", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("张阳", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont("签单日期", font10, 15, false, false, false);
            table.addCell(cell8);
            cell8 = mircoSoftFont(simpleDateFormat.format(new Date()), font10, 15, false, false, false);
            table.addCell(cell8);
            document.add(table);

            Paragraph paragraph15 = new Paragraph("总公司地址：中国上海市银城中路190号           邮政编码：200120                 网址：  http://www.cpic.com.cn", font10);
            paragraph15.setSpacingAfter(15);
            paragraph15.setSpacingBefore(1);
            document.add(paragraph15);

            Paragraph paragraph16 = new Paragraph("中国太平洋财产保险股份有限公司\n国内水路、陆路货物运输保险条款", font8Bold);
            paragraph16.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph16);

            Paragraph paragraph17 = new Paragraph("总则", font8Bold);
            paragraph17.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph17);

            Paragraph paragraph18 = new Paragraph("第一条 为使保险货物在水路、铁路、公路和联运运输中，因遭受保险责任范围内的自然灾害或意外事故所造成的损失能够得到经济补偿，并加强货物的安全防损工作，以利商品生产和商品流通，特举办保险。", font8);
            paragraph18.setFirstLineIndent(18f);
            document.add(paragraph18);

            Paragraph paragraph19 = new Paragraph("保险责任", font8Bold);
            paragraph19.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph19);

            Paragraph paragraph20 = new Paragraph("第二条 本保险分为基本险和综合险两种。保险货物遭受损失时，保险人按承保险别的责任范围负赔偿责任\n" + "（1）基本险\n" + "1. 因火灾、爆炸、雷电、冰雹、暴风、暴雨、洪水、地震、海啸、地陷、崖崩、滑坡、泥石流所造成的损失；\n" + "2. 由于运输工具发生碰撞、搁浅、触礁、倾覆、沉没、出轨或隧道、码头坍塌所造成的损失；\n" + "3. 在装货、卸货或转载时因遭受不属于包装质量不善或装卸人员违反操作规程所造成的损失；\n" + "4. 按国家统一或一般惯例应分摊的共同海损的费用；\n" + "5. 在发生上述灾害、事故时、因纷乱而造成货物的散失及因施救或保护货物所支付的直接合理的费用。", font8);
            paragraph20.setIndentationLeft(18f);
            document.add(paragraph20);

            Paragraph paragraph21 = new Paragraph("除外责任", font8Bold);
            paragraph21.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph21);

            Paragraph paragraph22 = new Paragraph("第三条 由于下列原因造成保险货物的损失，保险人不负赔偿责任\n" + "（一）战争或军事行动；\n" + "（二）核事件或核爆炸；\n" + "（三）保险货物本身的缺陷、自然损耗、以及货物包装不善；\n" + "（四）被保险人的故意行为或过失；\n" + "（五）全程是公路货物运输的，盗窃或整件提货不着的损失；\n" + "（六）其他不属于保险责任范围内的损失。", font8Bold);
            paragraph22.setIndentationLeft(18f);
            document.add(paragraph22);

            Paragraph paragraph23 = new Paragraph("责任起讫", font8Bold);
            paragraph23.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph23);

            Paragraph paragraph24 = new Paragraph("第四条 保险责任自签发保险凭证或保险货物运离起运地发货人的最后一个仓库或储运处所时起，" + "至该保险凭证上注明的目的地的收货人在当地的第一个仓库或储存处所时终止。但保险货物运抵目的地后，如果收货人未及时提货，则保险" + "责任的终止期最多延长至以收货人接到《到货通知单》后的十五天为限（以邮戳日期为准）。", font8);
            paragraph24.setFirstLineIndent(18f);
            document.add(paragraph24);

            Paragraph paragraph25 = new Paragraph("保险金额", font8Bold);
            paragraph25.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph25);

            Paragraph paragraph26 = new Paragraph("第五条 保险价值为货运的实际价值，按货物的实际价值或货物的实际价值加运杂费确定。保险金额由投保人" + "参照保险价值自行确定，并在保险合同中载明。保险金额不得超过保险价值。超过保险价值的，超过部分无效，保险人应当退还相应的保险费。", font8);
            paragraph26.setFirstLineIndent(18f);
            document.add(paragraph26);

            Paragraph paragraph27 = new Paragraph("投保人、被保险人的义务", font8Bold);
            paragraph27.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph27);

            Paragraph paragraph28 = new Paragraph("第五条 保险价值为货运的实际价值，按货物的实际价值或货物的实际价值加运杂费确定。保险金额由投保人" + "参照保险价值自行确定，并在保险合同中载明。保险金额不得超过保险价值。超过保险价值的，超过部分无效，保险人应当退还相应的保险费。", font8);
            paragraph28.setFirstLineIndent(18f);
            document.add(paragraph28);

            Paragraph paragraph29 = new Paragraph("投保人故意不履行如实告知义务的，保险人对于保险合同解除前发生的保险事故，" + "不承担赔偿责任，并不退还保险费。", font8Bold);
            paragraph29.setFirstLineIndent(18f);
            document.add(paragraph29);

            Paragraph paragraph30 = new Paragraph("投保人因重大过失未履行如实告知义务，对保险事故的发生有严重影响的，保险人对于保险合同解除前" + "发生的保险事故，不承担赔偿责任，但应当赔偿保险费。", font8Bold);
            paragraph30.setFirstLineIndent(18f);
            document.add(paragraph30);

            Paragraph paragraph31 = new Paragraph();
            Chunk chunk16 = new Chunk("第七条 投保人在保险人签发保险凭证的同时，应按照保险费率，一次交清应付的保险费。", font8);
            Chunk chunk17 = new Chunk("若投保人未按照约定交付保险费，保险费交付前发生的保险事故，保险人不承担赔偿责任。", font8Bold);
            paragraph31.add(chunk16);
            paragraph31.add(chunk17);
            paragraph31.setIndentationLeft(18f);
            document.add(paragraph31);

            Paragraph paragraph32 = new Paragraph();
            Chunk chunk18 = new Chunk("第八条 被保险人应严格遵守国家及交通运输部门关于安全运输的各项规定。还应当接受并协助" + "保险人对保险货物进行的查验防损工作，货物包装必须符合国家和主管部门规定的标准。", font8);
            Chunk chunk19 = new Chunk("对于因被保险人未遵守上述约定而导致保险事故的，保险人不负赔偿责任；对于因被保险人未遵守上述" + "约定而导致损失扩大的，保险人对扩大的损失不负赔偿责任。", font8Bold);
            paragraph32.add(chunk18);
            paragraph32.add(chunk19);
            paragraph32.setIndentationLeft(18f);
            document.add(paragraph32);

            Paragraph paragraph33 = new Paragraph("第九条 货物如果发生保险责任范围内的损失时，被保险人获悉后，应立即通知保险人的当地保险机构" + "并应迅速采取施救和保护措施防止或减少货物损失。", font8);
            paragraph33.setFirstLineIndent(18f);
            document.add(paragraph33);

            Paragraph paragraph34 = new Paragraph("故意或因重大过失未及时通知，致使保险事故的性质、原因、损失程度等难以确定的，保险人对无法确定的部分，" + "不承担赔偿责任，但保险人通过其他途径已经及时知道或者应当及时知道保险事故发生的除外。", font8Bold);
            paragraph34.setFirstLineIndent(18f);
            document.add(paragraph34);

            Paragraph paragraph35 = new Paragraph("赔偿处理", font8Bold);
            paragraph35.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph35);

            Paragraph paragraph36 = new Paragraph("第十条 被保险人向保险人申请索赔时，必须提供下列有关单证：\n" + "（一）保险凭证、运单（货票）、提货单、发货票；\n" + "（二）承诺部门签发的货运记录、普通记录、交接验收记录、鉴定书；\n" + "（三）收货单位的入库记录、检验报告、损失清单及救护货物所支付的直接费用的单据。", font8);
            paragraph36.setIndentationLeft(18f);
            document.add(paragraph36);

            Paragraph paragraph37 = new Paragraph("收到被保险人的赔偿请求后，应当及时就是否属于保险责任作出核定，并将核定结果通知被保险人。" + "情形复杂的，保险人在收到被保险人的赔偿请求并提供理赔所需资料后三十日内未能核定保险责任的保险人与被保险人根据实际情形商议合理" + "期间，保险人在商定的期间内作出核定结果并通知被保险人。对属于保险责任的，在与被保险人达成有关赔偿金额的协议后十日内履行赔偿义务。", font8);
            paragraph37.setFirstLineIndent(18f);
            document.add(paragraph37);

            Paragraph paragraph38 = new Paragraph("第十一条 货物发生保险责任范围内的损失时，按货价确定保险金额的，保险人根据实际损失按起运地货价计算赔偿；按货价加运杂费确定" + "保险金额的，保险人根据实际损失按起运地货价加运杂费计算。但最高赔偿金额以保险金额为限。", font8);
            paragraph38.setFirstLineIndent(18f);
            document.add(paragraph38);

            Paragraph paragraph39 = new Paragraph("第十二条 如果被保险人投保不足，保险金额低于货价时，保险人对其损失金额及支付的施救保护费用" + "按保险金额与货价的比例计算赔偿。保险人对货物损失的赔偿金额，以及因施救或保护货物所支付的直接、合理的费用，应分别计算，并各以不超过保险金额为限。", font8);
            paragraph39.setFirstLineIndent(18f);
            document.add(paragraph39);

            Paragraph paragraph40 = new Paragraph("第十三条 货物发生保险责任范围内的损失，如果根据法律规定或者有关约定，应当由承运人或其他第三者负责赔偿部分或全部的，被保险人" + "应首先向承运人或其他第三者索赔。如被保险人提出要求，保险人也可以先予赔偿，但被保险人应签发权益转让书给保险人，并协助保险人向责任方追偿。", font8);
            paragraph40.setFirstLineIndent(18f);
            document.add(paragraph40);

            Paragraph paragraph41 = new Paragraph("第十四条 经双方协商同意，保险人可将其享有的保险财产残余部分的权益作价折归被保险人，并可在保险赔偿金中直接扣除。\n" + "第十五条 被保险人与保险人发生争执时，应当实事求是，协商解决，双方不能达成协议时，可以提交仲裁机关或法院处理。\n" + "本保险合同适用中华人民共和国法律（不包括港澳台地区法律）。", font8);
            paragraph41.setIndentationLeft(18f);
            document.add(paragraph41);


            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    /**
     * 创建合同
     *
     * @param order
     * @param contractPath
     * @param fileName
     * @param contractLogoImg
     * @param contractSignImg
     * @param companyName
     * @throws IOException
     */
    public static void createConcatPDF(OrderVO order,
            String contractPath,
            String fileName,
            URL contractLogoImg,
            URL contractSignImg,
            String companyName) throws IOException {

        log.info("创建合同订单对象  {} {}", JSON.toJSON(order), order);

        //1.新建Document
        //页面大小 A4纸
        //Rectangle rect = new Rectangle(PageSize.A4.rotate());
        //rect.setBackgroundColor(BaseColor.GRAY);
        Document document = new Document();
//        document.setMargins(50,50,20,50);
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            Font font20 = new Font(baseFont, 20);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Font font8 = new Font(baseFont, 8, Font.NORMAL);
            String name = contractPath +  fileName + ".pdf";
            FileUtil.createDir(contractPath);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name));
            // 只读
            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            //writer.setPageEvent(new Watermark());
            document.open();
//            Image img = Image.getInstance(contractLogoImg);
//            img.setAlignment(Element.ALIGN_LEFT);
//            img.scaleToFit(150, 50);
//            document.add(img);
            LineSeparator line = new LineSeparator(1f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, -5f);
            document.add(line);
            // 合同单号
            Paragraph orderNo = new Paragraph("合同单号：" + order.getId(), font8);
            orderNo.setSpacingBefore(5f);
            orderNo.setAlignment(Element.ALIGN_RIGHT);
            document.add(orderNo);
            Paragraph title = new Paragraph(companyName + "合同单" + (order.getHasInsurance() == 1 ? "（含保险）" : "（不含保险）"), font20);
            title.setSpacingAfter(5f);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            document.add(line);

            Paragraph paragraph = new Paragraph("鉴于委托方已向本公司提交运车合同单，并同意按约定支付运输费用，本公司接受委托方的要求" + "，按合同所载条款，承担运输责任，特立本合同单", font10);
            paragraph.setSpacingBefore(5f);
            paragraph.setSpacingAfter(8f);
            paragraph.setFirstLineIndent(20);
            document.add(paragraph);

            LineSeparator line2 = new LineSeparator(1.5f, 100, BaseColor.GRAY, Element.ALIGN_CENTER, -7f);
            document.add(line);
            document.add(line2);

            PdfPTable table1 = new PdfPTable(2);
            table1.setWidthPercentage(100);
            table1.setTotalWidth(new float[]{0.5f, 0.5f});
            PdfPCell cell1_1 = mircoSoftFont("委托方：" + (StringUtils.isBlank(order.getCompanyName()) ? order.getLinkmanName() : order.getCompanyName()), font10, 25, false, true, false);
            cell1_1.disableBorderSide(15);
            table1.addCell(cell1_1);
            PdfPCell cell1_2 = mircoSoftFont("承运方：" + companyName, font10, 25, false, true, false);
            cell1_2.disableBorderSide(15);
            table1.addCell(cell1_2);
            table1.setSpacingBefore(5f);
            document.add(table1);


            document.add(line);
            document.add(line2);

            PdfPTable table2 = new PdfPTable(2);
            table2.setWidthPercentage(100);
            table2.setTotalWidth(new float[]{0.5f, 0.5f});
            PdfPCell cell2_1 = mircoSoftFont("出发地：" + order.getStartCode(), font10, 25, false, true, false);
            cell2_1.disableBorderSide(15);
            table2.addCell(cell2_1);
            PdfPCell cell2_2 = mircoSoftFont("目的地：" + order.getDestinationCode(), font10, 25, false, true, false);
            cell2_2.disableBorderSide(15);
            table2.addCell(cell2_2);
            table2.setSpacingBefore(5f);
            document.add(table2);

            // 发货人、收货人表格
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(new float[]{0.9f});
//            table.setLockedWidth(true);
            // 第一行
            PdfPCell cell1 = new PdfPCell();

            PdfPTable table3 = new PdfPTable(2);
            table3.setWidthPercentage(99);
            table3.setTotalWidth(new float[]{0.5f, 0.5f});
            PdfPCell cell3_1 = mircoSoftFont("发货人：" + order.getLinkmanName(), font10, 25, false, true, false);
            cell3_1.disableBorderSide(15);
            table3.addCell(cell3_1);
            PdfPCell cell3_2 = mircoSoftFont("联系电话：" + order.getLinkmanPhone(), font10, 25, false, true, false);
            cell3_2.disableBorderSide(15);
            table3.addCell(cell3_2);
            PdfPCell cell3_3 = mircoSoftFont("提车方式：" + PickCarTypeEnum.getValue(order.getPickType()), font10, 25, false, true, false);
            cell3_3.disableBorderSide(15);
            table3.addCell(cell3_3);
            PdfPCell cell3_4 = mircoSoftFont("提车地址：" + order.getPickAddress(), font10, 25, false, true, false);
            cell3_4.disableBorderSide(15);
            table3.addCell(cell3_4);
//            table3.setSpacingBefore(5f);
            cell1.addElement(table3);
            // 隐藏下边框
            cell1.disableBorderSide(2);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell();
            LineSeparator line3 = new LineSeparator(0.92f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, -5f);
            cell2.addElement(line3);
            cell2.disableBorderSide(15);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell();

            PdfPTable table4 = new PdfPTable(2);
            table4.setWidthPercentage(99);
            table4.setTotalWidth(new float[]{0.5f, 0.5f});
            PdfPCell cell4_1 = mircoSoftFont("收货人：" + order.getConsigneeName(), font10, 25, false, true, false);
            cell4_1.disableBorderSide(15);
            table4.addCell(cell4_1);
            PdfPCell cell4_2 = mircoSoftFont("联系电话：" + order.getConsigneePhone(), font10, 25, false, true, false);
            cell4_2.disableBorderSide(15);
            table4.addCell(cell4_2);
            PdfPCell cell4_3 = mircoSoftFont("送车方式：" + TakeCarTypeEnum.getValue(order.getTakeType()), font10, 25, false, true, false);
            cell4_3.disableBorderSide(15);
            table4.addCell(cell4_3);
            PdfPCell cell4_4 = mircoSoftFont("送车地址：" + order.getTakeAddress(), font10, 25, false, true, false);
            cell4_4.disableBorderSide(15);
            table4.addCell(cell4_4);
            cell3.addElement(table4);
            // 隐藏上边框
            cell3.disableBorderSide(1);
            table.addCell(cell3);

            document.add(table);

            PdfPTable table5 = new PdfPTable(5);
            table5.setWidthPercentage(100);
            table5.setTotalWidth(new float[]{0.2f, 0.3f, 0.1f, 0.2f, 0.2f});
            PdfPCell cell5_1 = mircoSoftFont("车型名称", font10, 25, true, true, false);
            table5.addCell(cell5_1);
            PdfPCell cell5_2 = mircoSoftFont("车牌/车架号", font10, 25, true, true, false);
            table5.addCell(cell5_2);
            PdfPCell cell5_3 = mircoSoftFont("数量", font10, 25, true, true, false);
            table5.addCell(cell5_3);
            PdfPCell cell5_4 = mircoSoftFont("车辆价值（万元）", font10, 25, true, true, false);
            table5.addCell(cell5_4);
            PdfPCell cell5_5 = mircoSoftFont("保险费（元）", font10, 25, true, true, false);
            table5.addCell(cell5_5);
            // 子订单内容
            for (OrderSub orderSub : order.getOrderSubList()) {
                table5.addCell(mircoSoftFont(orderSub.getCarName(), font10, 25, true, true, false));
                table5.addCell(mircoSoftFont(orderSub.getVin(), font10, 25, true, true, false));
                table5.addCell(mircoSoftFont("1台", font10, 25, true, true, false));

                table5.addCell(mircoSoftFont((orderSub.getCarCost() == null ? BigDecimal.ZERO : orderSub.getCarCost()).toString(), font10, 25, true, true, false));
                table5.addCell(mircoSoftFont((orderSub.getInsuranceFee() == null ? BigDecimal.ZERO : orderSub.getInsuranceFee()).toString(), font10, 25, true, true, false));
            }

            table5.setSpacingBefore(8f);
            document.add(table5);

            document.add(Chunk.NEWLINE);

            String totalFee = Convert.digitToChinese(order.getTotalFee());

            PdfPTable table6 = new PdfPTable(2);
            table6.setWidthPercentage(100);
            table6.setTotalWidth(new float[]{0.3f, 0.7f});
            PdfPCell cell6_2_1 = mircoSoftFont("总计：￥" + order.getTotalFee()
                    .toString() + "  （大写）人民币  " + totalFee, font10, 25, false, false, false);
            cell6_2_1.disableBorderSide(15);
            cell6_2_1.setColspan(2);
            table6.addCell(cell6_2_1);
//            PdfPCell cell6_2_2 = new PdfPCell();
//            cell6_2_2.disableBorderSide(15);
//            table6.addCell(cell6_2_2);
            BigDecimal hasPayFee = getHasPayFee(order);
            PdfPCell cell6_3_1 = mircoSoftFont("已付金额：￥" + hasPayFee.toString(), font10, 25, false, true, false);
            cell6_3_1.disableBorderSide(15);
            table6.addCell(cell6_3_1);
            PdfPCell cell6_3_2 = mircoSoftFont("未付金额：￥" + order.getTotalFee().subtract(hasPayFee)
                    .toString(), font10, 25, false, true, false);
            cell6_3_2.disableBorderSide(15);
            table6.addCell(cell6_3_2);
            table6.setSpacingBefore(5f);
            table6.setSpacingAfter(20f);
            document.add(table6);

            document.add(line);
            document.add(line2);

            PdfPTable table7 = new PdfPTable(2);
            table7.setWidthPercentage(100);
            table7.setTotalWidth(new float[]{0.5f, 0.5f});
            PdfPCell cell7_1 = mircoSoftFont("制单人：" + order.getCreateUserName(), font10, 25, false, true, false);
            cell7_1.disableBorderSide(15);
            table7.addCell(cell7_1);
            PdfPCell cell7_2 = new PdfPCell();
            Paragraph paragraph1 = new Paragraph("                             " , font10);
            Image image = Image.getInstance(contractSignImg);
            image.setAlignment(Image.UNDERLYING);
            image.scaleAbsolute(600, 600);
            Chunk chunk = new Chunk(image, -50, -60);
            paragraph1.add(chunk);
            cell7_2.addElement(paragraph1);
            cell7_2.disableBorderSide(15);
            table7.addCell(cell7_2);
            PdfPCell cell7_3 = mircoSoftFont("                                ", font10, 25, false, true, false);
            cell7_3.disableBorderSide(15);
            table7.addCell(cell7_3);
            PdfPCell cell7_4 = mircoSoftFont(simpleDateFormat.format(new Date()), font10, 25, false, true, false);
            cell7_4.disableBorderSide(15);
            table7.addCell(cell7_4);
            table7.setSpacingBefore(5f);
            document.add(table7);

            document.newPage();
            Paragraph paragraph2 = new Paragraph(companyName + "运输服务条款", font20);
            paragraph2.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph2);
            Paragraph paragraph4 = new Paragraph("保险及理赔事项", font10);
            paragraph4.setAlignment(Element.ALIGN_CENTER);
            paragraph4.setSpacingBefore(8f);
            paragraph4.setSpacingAfter(8f);
            document.add(paragraph4);
            if (order.getHasInsurance() == 1) {
                // 有保险
                Paragraph paragraph5 = new Paragraph("              第一条 甲方将商品车、二手车等（以下简称“车辆”）交由乙方承运的，应在运输前24小时内如实申报车辆的实际价格。\n" + "               第二条 运输保险费包含在运输费中，按甲乙双方指定的运输费结算方式进行支付。\n" + "               第三条 如遇运输过程中发生意外事故，导致车辆受损的，乙方将按如下方式进行赔偿：\n" + "               1. 如在运输过程中导致车辆剐蹭、掉漆、凹瘪等非结构性损伤的，乙方负责维修费，可更换件的更换新件。\n" + "               2. 如在运输过程中导致车辆的底盘、立柱、悬挂系统、驱动轮内部、发动机、变速箱等结构性损伤的，有如下两种解决方案：\n" + "                           2.1 未上过牌照的为全新商品车，乙方负责进行维修，并按照本合同订立时，甲方申报的车辆实际市场价格的降价10%进行赔偿《或买" + "断车辆所有权》。\n" + "                           2.2 如为二手车或者其他非全新商品车，乙方仅负责进行维修；不给与降价理赔。\n" + "               3. 如因如山洪、地震等不可抗力导致的车辆受损，乙方不负有赔偿责任，但本着服务至上的原则，甲乙双方友好协商，乙方可" + "给予不超过运输费总额三倍的补偿。\n" + "               4.非乙方原因导致的损伤，乙方不负有赔偿责任。\n" + "               第四条 车辆一旦发生受损事故的，甲方应当积极配合乙方进行理赔处理流程等相关事宜，如因甲方无法提供相应的材料或者不配合乙方进行" + "理赔处理的，所产生的理赔不到位或其他损失由甲方自行承担，如因乙方原因导致理赔不到位损失的乙方承担。", font8);
                document.add(paragraph5);

                Image image2 = Image.getInstance(contractSignImg);
                image2.setAlignment(Image.UNDERLYING);
                image2.scaleAbsolute(90, 90);
                image2.setAbsolutePosition(420f, 400f);
                document.add(image2);

                Paragraph paragraph6 = new Paragraph("违约及责任条款", font10);
                paragraph6.setAlignment(Element.ALIGN_CENTER);
                paragraph6.setSpacingBefore(8f);
                paragraph6.setSpacingAfter(8f);
                document.add(paragraph6);

                Paragraph paragraph7 = new Paragraph("              第一条 甲方指定联系人以外的其他第三人要求乙方承运车辆的，乙方有权拒绝承运，乙方不承担任何违约责任。\n" + "               第二条 乙方应在订单确认之日起，按照甲方的要求的实效内，及时提车、运车、交付，因天气、道路拥堵等不可抗力等因素影响的除外。\n" + "               第三条 乙方将车辆运输到指定地点后，由乙方工作人员以短信方式/微信方式向甲方指定联系人或甲方指定的收车人发送《收车通知》，甲方" + "指定收车人应于短信通知的时间完成收车。甲方指定收车人未能在通知的时间内完成收车的或者出现收车人拒收的，每逾期一日，甲方应向乙方支付" + "【每台300元/天】的车辆保管费，期间商品车毁损、灭失、盗抢及其它风险均由甲方承担。甲方拒绝支付运费、保管费的，乙方有权留置商品车并向 甲方主张相关权利。\n" + "               第四条 乙方将车辆运输到指定地点后，由乙方工作人员以短信方式/微信方式向甲方指定联系人发送《收车通知》，并将商品车到达目的地的" + "现场照片发送给甲方指定联系人，在乙方发送《收车通知》之时起一个自然日内，甲方没有异议的，视为乙方将商品车安全及时的运抵指定目的地。" + "甲方不得以任何理由追究乙方的违约责任。\n" + "               第五条 甲方指定收车人应携带其身份证原件并提交身份证原件、复印件办理收车事宜，否则乙方有权拒绝该收车人收车，乙方不构成迟延交" + "车的违约。甲方应向乙方支付【300元/天】的商品车保管费。待甲方指定收车人提供完备手续后，方可办理收车事宜。\n" + "               第六条 运输指令的变更，以甲方指定联系电话发送的短信为准。甲方确保在车辆运输期间合同单中指定联系电话由甲方使用，并自愿承担因" + "甲方联系电话发送错误运输指令造成运输错误的不利后果。\n" + "               第七条 甲方指定收车人信息的变更，以甲方重新指定联系人的电话或甲方指定收车人电话发送的短信为准。甲方确保在车辆运输期间合同单" + "中指定联系电话由甲方及甲方指定收车人使用，并自愿承担因甲方或甲方收车人联系电话发送错误指定收车人信息造成交车错误的不利后果。\n" + "               第八条 甲方指定收车人以外的其他人员要求乙方向其交付承运车辆的，应当向乙方出具由甲方指定联系电话发出的指定收车人变更的短信或" + "其他证明材料，乙方据此将车辆交付给变更后的指定收车人的，视为乙方已完成车辆交付义务。\n" + "               第九条 乙方有权根据自身实际情况将运输任务转委托给本合同以外的第三人，第三人执行转委托运输任务过程中发生车辆刮蹭、毁损、灭失" + "的，乙方在已投保的保险公司赔偿限额内向甲方承担赔偿责任。不足部分，甲方同意向本合同以外的第三人（或实际承运人）索赔。\n" + "               第十条 本合同目的为将商品车安全送至目的地，自乙方以微信方式/短信方式向甲方指定联系人或甲方指定的收车人发送《收车通知》后，甲" + "方本合同外的第三人，采用盗、抢或其他方式将商品车转移占有，不影响本合同目的已经实现，甲方不得就其损失或与第三人的债权债务关系向乙方" + "追偿或索赔。\n" + "               第十一条 车辆在交接运输过程中，乙方应对车辆外观、随车物品进行清点，并及时告知甲方，乙方对车辆的随车物品不负有保管义务（车辆" + "本身的说明书、车钥匙、备胎等配件或材料除外），因遗失导致的损失由甲方承担。\n" + "               第十二条  甲方委托运输的车辆来源需正规合法，车内不得私藏危险违禁品，如乙方发现以上情况，可以拒绝运输或向公安机关报案处理，如" + "乙方未发现以上情况，也不承担相应责任，且因以上情况给乙方造成损失的，甲方需要承担全部赔偿责任。", font8);
                paragraph7.setSpacingAfter(5f);
                document.add(paragraph7);
            } else {
                // 无保险
                Paragraph paragraph5 = new Paragraph("              第一条 甲方将商品车、二手车等（以下简称“车辆”）交由乙方承运的，本次运输不含车辆运输险。\n" + "               第二条 甲乙双方按指定的运输费结算方式进行支付运输费用，该费用不包含车辆运输保险费。\n" + "               第三条 如遇运输过程中发生意外事故，导致车辆受损的，双方将按如下方式进行处理：\n" + "               1. 车辆剐蹭、掉漆、凹瘪、底盘、立柱等损伤的，乙方赔偿甲方维修费，上限不超过三倍的运输费用。\n" + "               2. 如因如山洪、地震等不可抗力导致的车辆受损，乙方不负有赔偿责任。\n" + "               第四条 如甲方自行购买了运输险，一旦车辆发生任何程度的受损，甲方须自行进行理赔处理，乙方须积极配合。", font8);
                document.add(paragraph5);

                Image image2 = Image.getInstance(contractSignImg);
                image2.setAlignment(Image.UNDERLYING);
                image2.scaleAbsolute(90, 90);
                image2.setAbsolutePosition(420f, 400f);
                document.add(image2);

                Paragraph paragraph6 = new Paragraph("违约及责任条款", font10);
                paragraph6.setAlignment(Element.ALIGN_CENTER);
                paragraph6.setSpacingBefore(8f);
                paragraph6.setSpacingAfter(8f);
                document.add(paragraph6);

                Paragraph paragraph7 = new Paragraph("              第一条 甲方指定联系人以外的其他第三人要求乙方承运车辆的，乙方有权拒绝承运，乙方不承担任何违约责任。\n" + "               第二条 乙方应在订单确认之日起，按照甲方的要求的实效内，及时提车、运车、交付，因天气、道路拥堵等不可抗力等因素影响的除外。\n" + "               第三条 乙方将车辆运输到指定地点后，由乙方工作人员以短信方式/微信方式向甲方指定联系人或甲方指定的收车人发送《收车通知》，甲方指" + "定收车人应于短信通知的时间完成收车。甲方指定收车人未能在通知的时间内完成收车的或者出现收车人拒收的，每逾期一日，甲方应向乙方支付" + "【每台300元/天】的车辆保管费，期间商品车毁损、灭失、盗抢及其它风险均由甲方承担。甲方拒绝支付运费、保管费的，乙方有权留置商品车并向" + "甲方主张相关权利。\n" + "               第四条 乙方将车辆运输到指定地点后，由乙方工作人员以电话/短信/微信等方式向甲方指定联系人发送《收车通知》，并将商品车到达目的地" + "的现场照片发送给甲方指定联系人，在乙方发送《收车通知》之时起一个自然日内，甲方没有异议的，视为乙方将商品车安全及时的运抵指定目的地" + "。甲方不得以任何理由追究乙方的违约责任。\n" + "               第五条 甲方指定收车人应携带其身份证原件并提交身份证原件、复印件办理收车事宜，否则乙方有权拒绝该收车人收车，乙方不构成迟延交车" + "的违约。甲方应向乙方支付【300元/天】的商品车保管费。待甲方指定收车人提供完备手续后，方可办理收车事宜。\n" + "               第六条 运输指令的变更，以甲方指定联系电话发送的短信为准。甲方确保在车辆运输期间合同单中指定联系电话由甲方使用，并自愿承担因甲" + "方联系电话发送错误运输指令造成运输错误的不利后果。\n" + "               第七条 甲方指定收车人信息的变更，以甲方重新指定联系人的电话或甲方指定收车人电话发送的短信为准。甲方确保在车辆运输期间合同单中" + "指定联系电话由甲方及甲方指定收车人使用，并自愿承担因甲方或甲方收车人联系电话发送错误指定收车人信息造成交车错误的不利后果。\n" + "               第八条 甲方指定收车人以外的其他人员要求乙方向其交付承运车辆的，应当向乙方出具由甲方指定联系电话发出的指定收车人变更的短信或其" + "他证明材料，乙方据此将车辆交付给变更后的指定收车人的，视为乙方已完成车辆交付义务。\n" + "               第九条 乙方有权根据自身实际情况将运输任务转委托给本合同以外的第三人，第三人执行转委托运输任务过程中发生车辆刮蹭、毁损、灭失的" + "，甲方同意向本合同以外的第三人（或实际承运人）索赔。\n" + "               第十条 本合同目的为将商品车安全送至目的地，自乙方以微信方式/短信方式向甲方指定联系人或甲方指定的收车人发送《收车通知》后，甲方" + "本合同外的第三人，采用盗、抢或其他方式将商品车转移占有，不影响本合同目的已经实现，甲方不得就其损失或与第三人的债权债务关系向乙方追" + "偿或索赔。\n" + "               第十一条 车辆在交接运输过程中，乙方应对车辆外观、随车物品进行清点，并及时告知甲方。\n" + "               第十二条 甲方委托运输的车辆来源需正规合法，车内不得私藏危险违禁品，如乙方发现以上情况，可以拒绝运输或向公安机关报案处理，如乙" + "方未发现以上情况，也不承担相应责任，且因以上情况给乙方造成损失的，甲方需要承担全部赔偿责任。", font8);
                paragraph7.setSpacingAfter(5f);
                document.add(paragraph7);
            }


            document.add(line);
            document.add(line2);

            PdfPTable table8 = new PdfPTable(3);
            table8.setWidthPercentage(100);
            table8.setTotalWidth(new float[]{0.33f, 0.33f, 0.34f});
            String entrust = StringUtils.isBlank(order.getCompanyName()) ? order.getLinkmanName() : order.getCompanyName();
            PdfPCell cell8_1_1 = mircoSoftFont("委托方（甲方）：" + entrust, font10, 25, false, false, false);
            cell8_1_1.disableBorderSide(10);
            table8.addCell(cell8_1_1);
            PdfPCell cell8_1_2 = new PdfPCell();
            cell8_1_2.disableBorderSide(6);
            table8.addCell(cell8_1_2);
            PdfPCell cell8_1_3 = mircoSoftFont("委托方签字：", font10, 25, false, false, false);
            cell8_1_3.disableBorderSide(2);
            table8.addCell(cell8_1_3);
            PdfPCell cell8_2_1 = mircoSoftFont("联系电话：" + order.getLinkmanPhone(), font10, 25, false, false, false);
            cell8_2_1.disableBorderSide(9);
            table8.addCell(cell8_2_1);
            PdfPCell cell8_2_2 = mircoSoftFont("制单时间：" + simpleDateFormat.format(new Date()), font10, 25, false, false, false);
            cell8_2_2.disableBorderSide(5);
            table8.addCell(cell8_2_2);
            PdfPCell cell8_2_3 = new PdfPCell();
            cell8_2_3.disableBorderSide(1);
            table8.addCell(cell8_2_3);

            table8.setSpacingBefore(20f);
            document.add(table8);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    /**
     * 计算合计运费
     *
     * @param order
     * @return
     */
    //private static String getTotalTransportFee(PkgOrder order) {
    //    BigDecimal totalTransportFee = BigDecimal.ZERO;
    //    if (order.getTotalFee() != null) {
    //        totalTransportFee = totalTransportFee.add(order.getTotalFee());
    //    }
    //    if (order.getDiscountFee() != null) {
    //        totalTransportFee = totalTransportFee.add(order.getDiscountFee());
    //    }
    //    if (order.getInvoiceFee() != null) {
    //        totalTransportFee.subtract(order.getInvoiceFee());
    //    }
    //    return totalTransportFee.toString();
    //}
    private static BigDecimal getHasPayFee(OrderVO order) {
        BigDecimal hasPayFee = BigDecimal.ZERO;
        if (order.getOrderSubList() != null && order.getOrderSubList().size() > 0) {
            for (OrderSub orderSub : order.getOrderSubList()) {
                if (orderSub.getSpotPayFee() != null) {
                    hasPayFee = hasPayFee.add(orderSub.getSpotPayFee());
                }
            }
        }
        return hasPayFee;
    }


    /**
     * @param str  字符串
     * @param font 字体
     * @param high 表格高度
     * @return
     * @Param alignCenter 是否水平居中
     * @Param alignMidde  是否垂直居中
     * @Param haveColor 是否有背景色(灰色)
     */
    private static PdfPCell mircoSoftFont(String str,
            Font font,
            int high,
            boolean alignCenter,
            boolean alignMidde,
            boolean haveColor) {
        PdfPCell pdfPCell = new PdfPCell(new Phrase(str, font));
        pdfPCell.setMinimumHeight(high);
        pdfPCell.setUseAscender(true); // 设置可以居中
        if (alignCenter) {
            pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        }
        if (alignMidde) {
            pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        }
        if (haveColor) {
            //颜色代码 RGB
            pdfPCell.setBackgroundColor(BaseColor.RED);
        }
        return pdfPCell;
    }


    /**
     * 添加文字水印，并附加UUID
     *
     * @param srcFile  待加水印文件
     * @param destFile 加水印后输出文件
     * @param text     文本内容
     * @throws Exception
     */
    public static void addWaterMark(String srcFile, String destFile, String text) throws Exception {
        // 待加水印的文件
        PdfReader reader = new PdfReader(srcFile);
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));

        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;

        // 设置透明度
        PdfGState gs = new PdfGState();
        gs.setFillOpacity(0.3f);
        // 设置字体
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        // 循环对每页插入水印
        for (int i = 1; i < total; i++) {
            // 水印的起始
            content = stamper.getOverContent(i);
            content.setGState(gs);
            content.setFontAndSize(base, 32);
            // 开始
            content.beginText();
            // 设置颜色 默认为黑色
            content.setColorFill(BaseColor.BLACK);
            // 开始写入水印

            content.showTextAligned(Element.ALIGN_MIDDLE, text, 180, 340, 45);
//            content.showTextAligned(Element.ALIGN_MIDDLE, UUID.randomUUID().toString(), 140,
//                    240, 45);
            content.endText();
        }
        stamper.close();
    }

//    public static void main(String[] args) throws IOException {
//        PkgOrder order = new PkgOrder();
//        order.setId("09328-91242982424291081");
//        String path = "D:\\itext/";
////        ItextUtil.createPDFInsurance();
//        //ItextUtil.signPdf();
//    }

    /** 承运交接单 */
    public static void createPDFTransportLevel(PkgTransport transport,
            Map<String, Object> map,
            String contractPath,
            URL transportCardNumberImg, String companyName) {
        Document document = new Document();
        PkgTransportLevel transportLevel = transport.getTransportLevelList().get(0);
        OutputStream os = null;
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            String checked = "☑";
            String unChecked = "□";
            Font font20 = new Font(baseFont, 20);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Font font10Red = new Font(baseFont, 10, Font.NORMAL, BaseColor.RED);
            Font font8Red = new Font(baseFont, 8, Font.NORMAL, BaseColor.RED);
            Integer rowHigh = 15;

            FileUtil.createDir(contractPath);

            os = Files.newOutputStream(Paths.get(contractPath + map.get("fileName")));
            PdfWriter writer = PdfWriter.getInstance(document, os);
            // 只读
            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            document.open();
            Paragraph title = new Paragraph(companyName + "承运交接单", font20);
            title.setSpacingAfter(5f);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            Paragraph paragraph = new Paragraph(transport.getTransportNo() + "                     " + map.get("startCityName") + "-" + map.get("destinationCityName") + "                                                                                                     " + "制单日期：" + DateUtil.parseDateTime(transport.getTransportLevelList().get(0).getCreateTime()).toString(simpleDateFormat), font10);
            paragraph.setSpacingBefore(5f);
            paragraph.setSpacingAfter(8f);
            document.add(paragraph);

            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(new float[]{1f});
            // 第一行
            PdfPCell cell1 = new PdfPCell();
            cell1.disableBorderSide(15);
            PdfPTable table1 = new PdfPTable(3);
            table1.setWidthPercentage(100);
            table1.setTotalWidth(new float[]{0.2f, 0.3f, 0.5f});
            PdfPCell cell1_1 = mircoSoftFont("收货人：" + transportLevel.getReceiveUser(), font10, rowHigh, false, false, false);
            cell1_1.disableBorderSide(8);
            table1.addCell(cell1_1);
            PdfPCell cell1_3 = mircoSoftFont("联系电话：" + transportLevel.getReceivePhone(), font10, rowHigh, false, false, false);
            cell1_3.disableBorderSide(12);
            table1.addCell(cell1_3);
            PdfPCell cell1_4 = mircoSoftFont("收货地址：" + transportLevel.getReceiveAddress(), font10, rowHigh, false, false, false);
            cell1_4.disableBorderSide(4);
            table1.addCell(cell1_4);
            cell1.addElement(table1);
            table.addCell(cell1);
            // 循环分段中的订单
            for (int i = 0; i < transportLevel.getTransportLevelDetailList().size(); i++) {
                PdfPCell orderCell = new PdfPCell();
                PdfPTable orderTable = new PdfPTable(5);
                orderTable.setWidthPercentage(100);
                orderTable.setTotalWidth(new float[]{0.2f, 0.1f, 0.4f, 0.15f, 0.15f});
                PdfPCell orderCell1_1 = mircoSoftFont(transportLevel.getTransportLevelDetailList().get(i)
                        .getOrderCarVerify().getCarName(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_1);
                PdfPCell orderCell1_2 = mircoSoftFont(transportLevel.getTransportLevelDetailList().get(i)
                        .getOrderCarVerify().getKm() + "KM", font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_2);
                PdfPCell orderCell1_3 = mircoSoftFont(transportLevel.getTransportLevelDetailList().get(i)
                        .getOrderCarVerify().getVin(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_3);

                PdfPCell orderCell3 = mircoSoftFont("车辆外观及内饰描述：" + transportLevel.getTransportLevelDetailList()
                        .get(i).getOrderCarVerify().getRemark(), font10, rowHigh, false, false, false);
                orderCell3.setColspan(5);
                orderCell3.disableBorderSide(1);
                orderTable.addCell(orderCell3);
                orderCell.addElement(orderTable);
                orderCell.disableBorderSide(15);
                table.addCell(orderCell);
            }

            PdfPCell cell2 = new PdfPCell();
            PdfPTable table5 = new PdfPTable(3);
            table5.setWidthPercentage(100);
            table5.setTotalWidth(new float[]{0.3f, 0.3f, 0.4f});
            BigDecimal deliveryPayFee = BigDecimal.ZERO;
            for (TransportLevelDetail tld : transportLevel.getTransportLevelDetailList()) {
                if (tld.getOrderCarVerify().getDeliveryPayFee() != null) {
                    deliveryPayFee = deliveryPayFee.add(tld.getOrderCarVerify().getDeliveryPayFee());
                }
            }
            String deliveryPayFeeStr;
            if (deliveryPayFee.compareTo(BigDecimal.ZERO) > 0) {
                deliveryPayFeeStr = "￥" + deliveryPayFee + "       人民币：（大写）" + Convert.digitToChinese(deliveryPayFee);
            } else {
                deliveryPayFeeStr = "-";
            }
            PdfPCell cell5_0 = new PdfPCell(new Paragraph("到付金额：" + deliveryPayFeeStr, font10));
            cell5_0.setColspan(3);
            table5.addCell(cell5_0);
            PdfPCell cell5_1 = new PdfPCell(new Paragraph("协议事项：\n" + "1.商品车到达目的地后，接车人必须签字提车\n" + "2.商品车如运输过程中发生质损，托运方必须配合承运公司办理理赔手续，托运方不得擅自扣除运费抵充\n" + "3.如有以上违约，承运费可留置该商品车", font8Red));
            cell5_1.setColspan(2);
            table5.addCell(cell5_1);
            PdfPCell cell5_2 = new PdfPCell();
            cell5_2.setRowspan(3);
            Image image = Image.getInstance(transportCardNumberImg);
            cell5_2.setImage(image);
            cell5_2.setVerticalAlignment(Element.ALIGN_CENTER);
            table5.addCell(cell5_2);
            PdfPCell cell5_3 = new PdfPCell(new Paragraph("●特别声明：此车为贵重商品，托运人不参加投保的，如遇任何意外" + "所造成的经济损失本公司按运费的2倍予以赔偿", font8Red));
            cell5_3.setColspan(2);
            table5.addCell(cell5_3);
            PdfPCell cell5_4 = new PdfPCell(new Paragraph("承运司机签字：" + "\n" + "\n" + "\n" + "\n" + "\n" + "                                     年        月        日", font10));
            table5.addCell(cell5_4);
            PdfPCell cell5_5 = new PdfPCell(new Paragraph("收货人签字：" + "\n" + "\n" + "\n" + "\n" + "\n" + "                                     年        月        日", font10));
            table5.addCell(cell5_5);
            cell2.addElement(table5);
            cell2.disableBorderSide(15);
            table.addCell(cell2);

            document.add(table);

            Paragraph paragraph1 = new Paragraph("回单请邮寄至：安徽省合肥市滨湖新区万达城                电话：400-666-3166", font10);
            document.add(paragraph1);

            document.newPage();

            Paragraph paragraph2 = new Paragraph("运输商品车服务协议", font20);
            paragraph2.setSpacingAfter(20f);
            paragraph2.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph2);

            Paragraph paragraph3 = new Paragraph("依据相关法律规定，双方本着平等、自愿、公平之原则，经充分协商，就乙方向甲方提供运输乘用车服务事项达成" + "一致，为保障双方的利益，特达成如下协议：", font10);
            paragraph3.setFirstLineIndent(20);
            document.add(paragraph3);

            Paragraph paragraph4 = new Paragraph("一、甲方的权利、义务", font10);
            paragraph4.setFirstLineIndent(20);
            document.add(paragraph4);

            Paragraph paragraph5 = new Paragraph("1、甲方应认真、准确填写本单，确保所交运的车辆不涉及盗抢、套牌、走私等涉案不合法车辆，并能提供证明" + "该车辆的合法来源，使其适合运输。", font10);
            paragraph5.setFirstLineIndent(20);
            document.add(paragraph5);

            Paragraph paragraph6 = new Paragraph("2、甲方确保所托运的车辆内严禁携带易燃、易爆、易腐蚀、放射性以及国家法律明文规定的各种管制刀具、枪械、" + "毒品等物品。", font10);
            paragraph6.setFirstLineIndent(20);
            document.add(paragraph6);

            Paragraph paragraph7 = new Paragraph("3、甲方在车内放置的物品（≤200kg）需确保车辆的车窗、车门、后备箱能正常关闭，并确保能安全装卸上下" + "车，但不确保所搭载的物品不影响视线。", font10);
            paragraph7.setFirstLineIndent(20);
            document.add(paragraph7);

            Paragraph paragraph8 = new Paragraph("4、甲方托运的车辆内携带贵重物品要与乙方签订书面交接手续，如甲方刻意隐瞒不进行交接的，所造成的的任何后果甲方自行承担。", font10);
            paragraph8.setFirstLineIndent(20);
            document.add(paragraph8);

            Paragraph paragraph9 = new Paragraph("5、甲方可以在托运车辆期间随时查询车辆在运输途中的状态，但必须是乙方正常上班阶段。", font10);
            paragraph9.setFirstLineIndent(20);
            document.add(paragraph9);

            Paragraph paragraph10 = new Paragraph("6、甲方必须按照双方约定期限结算该次运输的相关费用（若到付，交车之前，甲方在目的地必须结清所有的费用，方可检查、接车。）", font10);
            paragraph10.setFirstLineIndent(20);
            document.add(paragraph10);

            Paragraph paragraph11 = new Paragraph("7、甲方可以对乙方或乙方授权人提出工作中的意见，乙方可以接受并加以改正。", font10);
            paragraph11.setFirstLineIndent(20);
            document.add(paragraph11);

            Paragraph paragraph12 = new Paragraph("二、乙方的权利、义务", font10);
            paragraph12.setFirstLineIndent(20);
            document.add(paragraph12);

            Paragraph paragraph13 = new Paragraph("1、乙方应认真、准确填写本单并按照上述约定的事项及时运输并准时完成运输事宜。", font10);
            paragraph13.setFirstLineIndent(20);
            document.add(paragraph13);

            Paragraph paragraph14 = new Paragraph("2、乙方按约定：如果甲方要求投保的，乙方须出具保单，甲方拒绝投保的，因交通意外、火灾爆炸、自然灾害、" + "雨淋、挤压、震动等造成的一切损失由甲方承担。", font10);
            paragraph14.setFirstLineIndent(20);
            document.add(paragraph14);

            Paragraph paragraph15 = new Paragraph("3、乙方有权检查被运输商品车辆的合法性并核对收车人的身份证等合法有效的证件。", font10);
            paragraph15.setFirstLineIndent(20);
            document.add(paragraph15);

            Paragraph paragraph16 = new Paragraph("4、乙方有乙方保证被运输的商品车辆在运输过程中不得被其他单位或个人使用并且有义务等待甲方查询积极" + "提供车辆在运输过程中所有的状态信息。", font10);
            paragraph16.setFirstLineIndent(20);
            document.add(paragraph16);

            Paragraph paragraph17 = new Paragraph("5、商品车辆在抵达目的地时，乙方有业务在第一时间通知甲方来提车并验车", font10);
            paragraph17.setFirstLineIndent(20);
            document.add(paragraph17);

            Paragraph paragraph18 = new Paragraph("三、保密条款", font10);
            paragraph18.setFirstLineIndent(20);
            document.add(paragraph18);

            Paragraph paragraph19 = new Paragraph("1、甲、乙双方从对方获取的资料和相关的商业机密、无需公开的信息等，双方负有保密义务，并应采取一切合法" + "的措施以使其所有接受的资料免于被无关人员接触。", font10);
            paragraph19.setFirstLineIndent(20);
            document.add(paragraph19);

            Paragraph paragraph20 = new Paragraph("2、此信息仅供委托人使用，如果不是预期的委托人，特此声明，对于本承运单的复印、印刷等任何形式使用或" + "依此产生的任何行动都是严格禁止和无效的。", font10);
            document.add(paragraph20);

            Paragraph paragraph21 = new Paragraph("四、保险条款", font10);
            paragraph21.setFirstLineIndent(20);
            document.add(paragraph21);

            Paragraph paragraph22 = new Paragraph("1、商品车辆运输的保险由太平洋、中国平安、中国人民财产保险等保险公司全程承保，保险金额为车辆实际价格，最高" + "不超过市场的销售价格。", font10);
            paragraph22.setFirstLineIndent(20);
            document.add(paragraph22);

            Paragraph paragraph23 = new Paragraph("五、责任索赔", font10);
            paragraph23.setFirstLineIndent(20);
            document.add(paragraph23);

            Paragraph paragraph24 = new Paragraph("1、商品车辆运达目的地并经接收方签字前，若甲方已经自行办理的运输保险或者委托乙方代办运输保险，因运输" + "原因导致甲方车辆出现划痕、碰撞、弯曲、开裂等现象，保险公司承担责任，乙方不承担责任但有义务协助甲方向保险公司" + "提车车辆实际的申报向保险公司索要赔偿，但运费不免。", font10);
            paragraph24.setFirstLineIndent(20);
            document.add(paragraph24);

            Paragraph paragraph25 = new Paragraph("2、如运输的商品车辆没有办理运输保险或者没有委托乙方办理保险的，车辆一旦发生划痕、碰撞等" + "现象，乙方将负责车辆维修，但维修费用最高不超过甲方支付给乙方的运费2倍（运输费用全额付清给乙方后再进行维修）。", font10);
            paragraph25.setFirstLineIndent(20);
            document.add(paragraph25);

            Paragraph paragraph26 = new Paragraph("六、其他", font10);
            paragraph26.setFirstLineIndent(20);
            document.add(paragraph26);

            Paragraph paragraph27 = new Paragraph("1、任何乙方欲解除运输必须以书面形式提前通知对方，运输结束后，合同自然解除。协议解除后尚有待结运费的，" + "甲方在接到结算通知后5日内结清。", font10);
            paragraph27.setFirstLineIndent(20);
            document.add(paragraph27);

            Paragraph paragraph28 = new Paragraph("2、本协议双方各执一份，自双方盖章之日生效，甲、乙双方在履行过程中发生任何争议应及时沟通解决，" + "如不能协商解决的可向合肥市包河区人民法院提起诉讼。", font10);
            paragraph28.setFirstLineIndent(20);
            document.add(paragraph28);



            document.close();
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();

            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //private static String dealVerifyInfo(PkgOrderCarVerify orderCarVerify) {
    //    StringBuilder sb = new StringBuilder();
    //    if (orderCarVerify.getSpecification() != null) {
    //        sb.append("说明书："+orderCarVerify.getSpecification()+"  本       ");
    //    }
    //    if (orderCarVerify.getTowHook() != null) {
    //        sb.append("拖车钩："+orderCarVerify.getTowHook()+"  个       ");
    //    }
    //    if (orderCarVerify.getAshtray() != null) {
    //        sb.append("烟灰缸："+orderCarVerify.getAshtray()+"  个       ");
    //    }
    //    if (orderCarVerify.getToolkit() != null) {
    //        sb.append("工具包："+orderCarVerify.getToolkit()+"  个       ");
    //    }
    //    if (orderCarVerify.getJack() != null) {
    //        sb.append("千斤顶："+orderCarVerify.getJack()+"  个       ");
    //    }
    //    if (orderCarVerify.getWarningSign() != null) {
    //        sb.append("警示牌："+orderCarVerify.getWarningSign()+"  个       ");
    //    }
    //    if (orderCarVerify.getLicensePlateFrame() != null) {
    //        sb.append("牌照架："+orderCarVerify.getLicensePlateFrame()+"  个       \n");
    //    }
    //    if (orderCarVerify.getCigarLighter() != null) {
    //        sb.append("点烟器："+orderCarVerify.getCigarLighter()+"  个       ");
    //    }
    //    if (orderCarVerify.getAidPacket() != null) {
    //        sb.append("急救包："+orderCarVerify.getAidPacket()+"  个       ");
    //    }
    //    if (orderCarVerify.getBellCover() != null) {
    //        sb.append("胎铃盖："+orderCarVerify.getBellCover()+"  个       ");
    //    }
    //    if (orderCarVerify.getAirPump() != null) {
    //        sb.append("气     泵：" + orderCarVerify.getAirPump() + "  个       ");
    //    }
    //    if (orderCarVerify.getAntenna() != null) {
    //        sb.append("天     线：" + orderCarVerify.getAntenna() + "  个       ");
    //    }
    //    if (orderCarVerify.getFootPad() != null) {
    //        sb.append("脚     垫：" + orderCarVerify.getFootPad() + "  个       ");
    //    }
    //    return sb.toString();
    //}

    private static float getGoodsPaddingTop(int size) {
        float result = 1f;
//        switch (size) {
//            case 1 :
//                result  =  440f;
//                break;
//            case 2 :
//                result  =  180f;
//                break;
//            case 3:
//                result  =  90f;
//                break;
//            case 4:
//                result  =  60f;
//                break;
//            case 5:
//                result  =  45f;
//                break;
//            case 6:
//                result  =  30f;
//                break;
//            case 7:
//                result  =  15f;
//                break;
//            case 8:
//                result  =  1f;
//                break;
//        }
        return result;
    }


    /** 交付单 */
    public static void createPDFOrderFinishRecord(List<PkgOrderSub> orderSubList,
            Map<String, Object> map,
            String companyName, String fileName) {
        Document document = new Document();
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font20 = new Font(baseFont, 20);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Integer rowHigh = 15;
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            // 只读
            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            document.open();
            Paragraph title = new Paragraph(companyName + "交付单", font20);
            title.setSpacingAfter(5f);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            Paragraph paragraph = new Paragraph(map.get("startCityName") + "→" + map.get("destinationCityName") + "                  送车方式：" + TakeCarTypeEnum.getValue(orderSubList.get(0).getTakeType()) + "                  制单人：" + LoginContextHolder.me().getSysLoginUserAccount() + "                  制单日期：" + simpleDateFormat.format(new Date()), font10);
            paragraph.setSpacingBefore(5f);
            paragraph.setSpacingAfter(8f);
            document.add(paragraph);

            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(new float[]{1f});
            // 第一行
            PdfPCell cell1 = new PdfPCell();
            cell1.disableBorderSide(15);
            PdfPTable table1 = new PdfPTable(3);
            table1.setWidthPercentage(100);
            table1.setTotalWidth(new float[]{0.2f, 0.3f, 0.5f});
            PdfPCell cell1_1 = mircoSoftFont("收货人：" + orderSubList.get(0).getConsigneeName(), font10, rowHigh, false, false, false);
            cell1_1.disableBorderSide(8);
            table1.addCell(cell1_1);
//            PdfPCell cell1_2 = mircoSoftFont("证件号：" ,font10,rowHigh,false,false,false);
//            cell1_2.disableBorderSide(12);
//            table1.addCell(cell1_2);
            PdfPCell cell1_3 = mircoSoftFont("联系电话：" + orderSubList.get(0).getConsigneePhone(), font10, rowHigh, false, false, false);
            cell1_3.disableBorderSide(12);
            table1.addCell(cell1_3);
            PdfPCell cell1_4 = mircoSoftFont("收货地址：" + orderSubList.get(0).getTakeAddress(), font10, rowHigh, false, false, false);
            cell1_4.disableBorderSide(4);
            table1.addCell(cell1_4);
            cell1.addElement(table1);
            table.addCell(cell1);
            // 循环分段中的订单
            for (int i = 0; i < orderSubList.size(); i++) {
                PdfPCell orderCell = new PdfPCell();
                PdfPTable orderTable = new PdfPTable(5);
                orderTable.setWidthPercentage(100);
                orderTable.setTotalWidth(new float[]{0.3f, 0.1f, 0.3f, 0.15f, 0.15f});
                PdfPCell orderCell1_1 = mircoSoftFont(orderSubList.get(i).getOrderSubNo() + "     " + orderSubList.get(i).getCarName(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_1);
                PdfPCell orderCell1_2 = mircoSoftFont(orderSubList.get(i).getOrderCarVerify().getKm() + "KM", font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_2);
                PdfPCell orderCell1_3 = mircoSoftFont(orderSubList.get(i).getVin(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_3);
                //String key = orderSubList.get(i).getOrderCarVerify().getKey() == null ? "-" : orderSubList.get(i).getOrderCarVerify().getKey().toString();
                //PdfPCell orderCell1_4 = mircoSoftFont("钥匙：" + key,font10,rowHigh,false,false,false);
                //orderTable.addCell(orderCell1_4);
                //String spareTire = orderSubList.get(i).getOrderCarVerify().getSpareTire() == null ? "-" : orderSubList.get(i).getOrderCarVerify().getSpareTire().toString();
                //PdfPCell orderCell1_5 = mircoSoftFont("备胎：" + spareTire,font10,rowHigh,false,false,false);
                //orderTable.addCell(orderCell1_5);
                // 处理随车物品拼接
                //String verifyInfo = dealVerifyInfo(orderSubList.get(i).getOrderCarVerify());
                //PdfPCell orderCell2 = mircoSoftFont(verifyInfo,font10,rowHigh,false,false,false);
                //orderCell2.setColspan(5);
                //orderCell2.setPaddingTop(getGoodsPaddingTop(orderSubList.size()));
                //orderTable.addCell(orderCell2);
                PdfPCell orderCell3 = mircoSoftFont("车辆外观及内饰描述：" + orderSubList.get(i).getOrderCarVerify().getRemark(), font10, rowHigh, false, false, false);
                orderCell3.setColspan(5);
                orderCell3.disableBorderSide(1);
                orderTable.addCell(orderCell3);
                orderCell.addElement(orderTable);
                orderCell.disableBorderSide(15);
                table.addCell(orderCell);
            }

            // table5
            PdfPCell cell2 = new PdfPCell();
            PdfPTable table5 = new PdfPTable(3);
            table5.setWidthPercentage(100);
            table5.setTotalWidth(new float[]{0.2f, 0.4f, 0.4f});
            BigDecimal deliveryPayFee = BigDecimal.ZERO;
            for (PkgOrderSub orderSub : orderSubList) {
                if (orderSub.getDeliveryPayFee() != null) {
                    deliveryPayFee = deliveryPayFee.add(orderSub.getDeliveryPayFee());
                }
            }
            String deliveryPayFeeStr;
            if (deliveryPayFee.compareTo(BigDecimal.ZERO) > 0) {
                deliveryPayFeeStr = Convert.digitToChinese(deliveryPayFee);
            } else {
                deliveryPayFeeStr = "-";
            }

            PdfPCell cell5_0 = mircoSoftFont("到付金额（大写）", font10, rowHigh, true, false, false);
            table5.addCell(cell5_0);

            PdfPCell cell5_1 = mircoSoftFont(deliveryPayFeeStr, font10, rowHigh, true, false, false);
            table5.addCell(cell5_1);

            PdfPCell cell5_5 = mircoSoftFont("车辆完好无损", font10, rowHigh, false, true, false);
            table5.addCell(cell5_5);

            PdfPCell cell5_3 = mircoSoftFont("小写", font10, rowHigh, true, false, false);
            table5.addCell(cell5_3);

            PdfPCell cell5_4 = mircoSoftFont(deliveryPayFee.toString(), font10, rowHigh, true, false, false);
            table5.addCell(cell5_4);

            PdfPCell cell5_2 = mircoSoftFont("客户签字：                                                  年    月    日", font10, rowHigh, false, true, false);
            //cell5_2.setRowspan(2);
            table5.addCell(cell5_2);

            cell2.addElement(table5);
            cell2.disableBorderSide(15);
            table.addCell(cell2);

            document.add(table);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /** 提车单 */
    public static void createPDFBillOfLadingRecord(PkgOrderPick pkgOrderPick, Map<String, Object> map, String companyName, String fileName) {
        Document document = new Document();
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font20 = new Font(baseFont, 20);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Integer rowHigh = 15;
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            // 只读
            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            document.open();
            Paragraph title = new Paragraph(companyName + "提车单", font20);
            title.setSpacingAfter(5f);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            Paragraph paragraph = new Paragraph(map.get("startCityName") + "→" + map.get("destinationCityName") + "                  提车方式：" + PickCarTypeEnum.getValue(pkgOrderPick.getPickType()) + "                  制单人：" + LoginContextHolder.me().getSysLoginUser().getName() + "                  制单日期：" + simpleDateFormat.format(new Date()), font10);
            paragraph.setSpacingBefore(5f);
            paragraph.setSpacingAfter(8f);
            document.add(paragraph);

            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(new float[]{1f});

            //<editor-fold desc="cell1">
            // 第一行
            PdfPCell cell1 = new PdfPCell();
            cell1.disableBorderSide(15);

            PdfPTable table1 = new PdfPTable(3);
            table1.setWidthPercentage(100);
            table1.setTotalWidth(new float[]{0.2f, 0.3f, 0.5f});
            PdfPCell cell1_1 = mircoSoftFont("提车联系人：" + pkgOrderPick.getPickLinkmanName(), font10, rowHigh, false, false, false);
            cell1_1.disableBorderSide(8);
            table1.addCell(cell1_1);

            PdfPCell cell1_2 = mircoSoftFont("提车联系人电话：" + pkgOrderPick.getPickLinkmanPhone(), font10, rowHigh, false, false, false);
            cell1_2.disableBorderSide(12);
            table1.addCell(cell1_2);

            String date = "无";
            Date appointPickTime = pkgOrderPick.getAppointPickTime();
            if (null != appointPickTime) {
                date = simpleDateFormat1.format(appointPickTime);
            }
            PdfPCell cell1_3 = mircoSoftFont("预约提车时间：" + date, font10, rowHigh, false, false, false);
            cell1_3.disableBorderSide(4);
            table1.addCell(cell1_3);

            cell1.addElement(table1);
            table.addCell(cell1);
            //</editor-fold>

            //<editor-fold desc="orderCell">
            PdfPCell orderCell = new PdfPCell();
            orderCell.disableBorderSide(15);

            PdfPTable orderTable = new PdfPTable(2);
            orderTable.setWidthPercentage(100);
            orderTable.setTotalWidth(new float[]{0.5f, 0.5f});

            PdfPCell orderCell1_1 = mircoSoftFont(pkgOrderPick.getOrderSubNo() + "     " + pkgOrderPick.getCarName() + "     " + pkgOrderPick.getVin(), font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell1_1);

            String pickCompanyName = pkgOrderPick.getCompanyName();
            if (StringUtils.isBlank(pickCompanyName)) pickCompanyName = "无";
            PdfPCell orderCell1_2 = mircoSoftFont("提车公司名称:" + pickCompanyName, font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell1_2);

            PdfPCell orderCell2_1 = mircoSoftFont("司机姓名:" + pkgOrderPick.getDriverName(), font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell2_1);

            PdfPCell orderCell2_2 = mircoSoftFont("司机电话：" + pkgOrderPick.getDriverPhone(), font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell2_2);

            PdfPCell orderCell3_1 = mircoSoftFont("结算人:" + pkgOrderPick.getSettlementName(), font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell3_1);

            PdfPCell orderCell3_2 = mircoSoftFont("结算人电话：" + pkgOrderPick.getSettlementPhone(), font10, rowHigh, false, false, false);
            orderTable.addCell(orderCell3_2);

            orderCell.addElement(orderTable);
            table.addCell(orderCell);
            //</editor-fold>

            //<editor-fold desc="cell2">
            // table5
            PdfPCell cell2 = new PdfPCell();

            PdfPTable table5 = new PdfPTable(3);
            table5.setWidthPercentage(100);
            table5.setTotalWidth(new float[]{0.2f, 0.4f, 0.4f});

            BigDecimal deliveryPayFee = BigDecimal.ZERO;
            if (pkgOrderPick.getPickCarFee() != null) {
                deliveryPayFee = deliveryPayFee.add(pkgOrderPick.getPickCarFee());
            }
            String deliveryPayFeeStr;
            if (deliveryPayFee.compareTo(BigDecimal.ZERO) > 0) {
                deliveryPayFeeStr = Convert.digitToChinese(deliveryPayFee);
            } else {
                deliveryPayFeeStr = "-";
            }

            PdfPCell cell5_0 = mircoSoftFont("提车金额（大写）", font10, rowHigh, true, false, false);
            table5.addCell(cell5_0);

            PdfPCell cell5_1 = mircoSoftFont(deliveryPayFeeStr, font10, rowHigh, true, false, false);
            table5.addCell(cell5_1);

            PdfPCell cell5_5 = mircoSoftFont("车辆完好无损", font10, rowHigh, false, true, false);
            cell5_5.disableBorderSide(Rectangle.BOTTOM);
            table5.addCell(cell5_5);

            PdfPCell cell5_3 = mircoSoftFont("小写", font10, rowHigh, true, false, false);
            table5.addCell(cell5_3);

            PdfPCell cell5_4 = mircoSoftFont(deliveryPayFee.toPlainString(), font10, rowHigh, true, false, false);
            table5.addCell(cell5_4);

            PdfPCell cell5_2 = mircoSoftFont("司机签字：                                                  年    月    日", font10, rowHigh, false, true, false);
            //cell5_2.setRowspan(2);
            cell5_2.disableBorderSide(Rectangle.TOP);
            table5.addCell(cell5_2);

            cell2.addElement(table5);
            cell2.disableBorderSide(15);
            table.addCell(cell2);
            //</editor-fold>

            document.add(table);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /** 送车单 */
    public static void createPDFVehicleDeliverySlipGeneration(List<PkgOrderDelivery> pkgOrderPick, Map<String, Object> map, String companyName, String fileName) {
        Document document = new Document();
        try {
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font20 = new Font(baseFont, 20);
            Font font10 = new Font(baseFont, 10, Font.NORMAL);
            Integer rowHigh = 15;
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
            // 只读
            writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            document.open();
            Paragraph title = new Paragraph(companyName + "送车单", font20);
            title.setSpacingAfter(5f);
            title.setAlignment(Element.ALIGN_CENTER);
            document.add(title);

            Paragraph paragraph = new Paragraph(map.get("startCityName") + "→" + map.get("destinationCityName") + "                  送车方式：" + PickCarTypeEnum.getValue(pkgOrderPick.get(0).getTakeType()) + "                  制单人：" + LoginContextHolder.me().getSysLoginUser().getName() + "                  制单日期：" + simpleDateFormat.format(new Date()), font10);
            paragraph.setSpacingBefore(5f);
            paragraph.setSpacingAfter(8f);
            document.add(paragraph);

            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(new float[]{1f});

            //<editor-fold desc="cell1">
            // 第一行
            PdfPCell cell1 = new PdfPCell();
            cell1.disableBorderSide(15);

            PdfPTable table1 = new PdfPTable(3);
            table1.setWidthPercentage(100);
            table1.setTotalWidth(new float[]{0.2f, 0.3f, 0.5f});
            PdfPCell cell1_1 = mircoSoftFont("收货人：" + pkgOrderPick.get(0).getConsigneeName(), font10, rowHigh, false, false, false);
            cell1_1.disableBorderSide(8);
            table1.addCell(cell1_1);

            PdfPCell cell1_2 = mircoSoftFont("收货人电话：" + pkgOrderPick.get(0).getConsigneePhone(), font10, rowHigh, false, false, false);
            cell1_2.disableBorderSide(12);
            table1.addCell(cell1_2);

            String date = "无";
            PdfPCell cell1_3 = mircoSoftFont("", font10, rowHigh, false, false, false);
            cell1_3.disableBorderSide(4);
            table1.addCell(cell1_3);

            cell1.addElement(table1);
            table.addCell(cell1);
            //</editor-fold>

            //<editor-fold desc="orderCell">

            for (int i = 0; i < pkgOrderPick.size(); i++) {


                PdfPCell orderCell = new PdfPCell();
                orderCell.disableBorderSide(15);

                PdfPTable orderTable = new PdfPTable(2);
                orderTable.setWidthPercentage(100);
                orderTable.setTotalWidth(new float[]{0.5f, 0.5f});

                PdfPCell orderCell1_1 = mircoSoftFont(pkgOrderPick.get(i).getOrderSubNo() + "     " + pkgOrderPick.get(i).getCarName() + "     " + pkgOrderPick.get(i).getVin(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_1);

                PdfPCell orderCell1_2 = mircoSoftFont("承运费:" + pkgOrderPick.get(i).getTransportCost(), font10, rowHigh, false, false, false);
                orderTable.addCell(orderCell1_2);

                //String pickCompanyName = pkgOrderPick.get(i).getCompanyName();
                //if (StringUtils.isBlank(pickCompanyName)) pickCompanyName = "无";
                //PdfPCell orderCell1_2 = mircoSoftFont("送车公司名称:" + pickCompanyName, font10, rowHigh, false, false, false);
                //orderTable.addCell(orderCell1_2);
                //
                //PdfPCell orderCell2_1 = mircoSoftFont("司机姓名:" + pkgOrderPick.get(i).getDriverName(), font10, rowHigh, false, false, false);
                //orderTable.addCell(orderCell2_1);
                //
                //PdfPCell orderCell2_2 = mircoSoftFont("司机电话：" + pkgOrderPick.get(i).getDriverPhone(), font10, rowHigh, false, false, false);
                //orderTable.addCell(orderCell2_2);
                //
                //PdfPCell orderCell3_1 = mircoSoftFont("结算人:" + pkgOrderPick.get(i).getSettlementName(), font10, rowHigh, false, false, false);
                //orderTable.addCell(orderCell3_1);
                //
                //PdfPCell orderCell3_2 = mircoSoftFont("结算人电话：" + pkgOrderPick.get(i).getSettlementPhone(), font10, rowHigh, false, false, false);
                //orderTable.addCell(orderCell3_2);

                orderCell.addElement(orderTable);
                table.addCell(orderCell);

            }


            //</editor-fold>

            //<editor-fold desc="cell2">
            // table5
            PdfPCell cell2 = new PdfPCell();

            PdfPTable table5 = new PdfPTable(3);
            table5.setWidthPercentage(100);
            table5.setTotalWidth(new float[]{0.2f, 0.4f, 0.4f});

            BigDecimal deliveryPayFee = BigDecimal.ZERO;
            for (int i = 0; i < pkgOrderPick.size(); i++) {
                if (pkgOrderPick.get(i).getTransportCost() != null) {
                    deliveryPayFee = deliveryPayFee.add(pkgOrderPick.get(i).getTransportCost());
                }
            }

            String deliveryPayFeeStr;
            if (deliveryPayFee.compareTo(BigDecimal.ZERO) > 0) {
                deliveryPayFeeStr = Convert.digitToChinese(deliveryPayFee);
            } else {
                deliveryPayFeeStr = "-";
            }

            PdfPCell cell5_0 = mircoSoftFont("承运费金额（大写）", font10, rowHigh, true, false, false);
            table5.addCell(cell5_0);

            PdfPCell cell5_1 = mircoSoftFont(deliveryPayFeeStr, font10, rowHigh, true, false, false);
            table5.addCell(cell5_1);

            PdfPCell cell5_5 = mircoSoftFont("车辆完好无损", font10, rowHigh, false, true, false);
            cell5_5.disableBorderSide(Rectangle.BOTTOM);
            table5.addCell(cell5_5);

            PdfPCell cell5_3 = mircoSoftFont("小写", font10, rowHigh, true, false, false);
            table5.addCell(cell5_3);

            PdfPCell cell5_4 = mircoSoftFont(deliveryPayFee.toPlainString(), font10, rowHigh, true, false, false);
            table5.addCell(cell5_4);

            PdfPCell cell5_2 = mircoSoftFont("客户签字：                                                  年    月    日", font10, rowHigh, false, true, false);
            //cell5_2.setRowspan(2);
            cell5_2.disableBorderSide(Rectangle.TOP);
            table5.addCell(cell5_2);

            cell2.addElement(table5);
            cell2.disableBorderSide(15);
            table.addCell(cell2);
            //</editor-fold>

            document.add(table);

            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
